package com.git.hui.yinshua.biz.goods.service;

import com.git.hui.yinshua.api.model.ListRsp;
import com.git.hui.yinshua.api.model.constants.GlobalConstants;
import com.git.hui.yinshua.biz.goods.dao.domain.GoodsDetailEntity;
import com.git.hui.yinshua.biz.goods.dao.domain.GoodsEntity;
import com.git.hui.yinshua.biz.goods.dao.domain.GoodsSpecEntity;
import com.git.hui.yinshua.biz.goods.dao.repository.GoodsRepository;
import com.git.hui.yinshua.biz.goods.service.convert.GoodsConvert;
import com.git.hui.yinshua.biz.rest.models.goods.req.GoodsQueryReq;
import com.git.hui.yinshua.biz.rest.models.goods.req.OperateGoodsReq;
import com.git.hui.yinshua.biz.rest.models.goods.rsp.GoodsDetailDto;
import com.git.hui.yinshua.biz.rest.models.goods.rsp.GoodsInfoDto;
import com.git.hui.yinshua.biz.rest.models.goods.rsp.GoodsSpecDto;
import com.git.hui.yinshua.biz.util.DiffUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author YiHui
 * @date 2024/6/13
 */
@Service
public class GoodsService {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private GoodsRepository goodsRepository;

    /**
     * 添加商品
     *
     * @param req
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean add(OperateGoodsReq req) {
        GoodsEntity goods = goodsRepository.addGoods(req);
        goodsRepository.addDetails(goods, req.getDetailList());
        goodsRepository.addGoodsSpecs(goods, req.getSpecList());
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean update(OperateGoodsReq req) {
        goodsRepository.updateGoods(req);
        GoodsEntity dbRecord = goodsRepository.queryGoodInfo(req.getGoodsId());

        List<GoodsDetailDto> newDetails = req.getDetailList();
        if (newDetails == null) newDetails = new ArrayList<>();
        DiffUtil.DiffRes<GoodsDetailDto> detailDiffRes = DiffUtil.diffRecord(dbRecord.getDetailList(), newDetails, GoodsDetailEntity::getId, GoodsDetailDto::getDetailId);
        goodsRepository.addDetails(dbRecord, detailDiffRes.getAdds());
        goodsRepository.updateDetails(dbRecord.getId(), detailDiffRes.getUpdates());
        goodsRepository.deleteDetails(detailDiffRes.getDeletes());

        List<GoodsSpecDto> newSpecs = req.getSpecList();
        if (newSpecs == null) newSpecs = new ArrayList<>();
        DiffUtil.DiffRes<GoodsSpecDto> specDiffRes = DiffUtil.diffRecord(dbRecord.getSpecList(), newSpecs, GoodsSpecEntity::getId, GoodsSpecDto::getSpecId);
        goodsRepository.addGoodsSpecs(dbRecord, specDiffRes.getAdds());
        goodsRepository.updateSpecs(dbRecord.getId(), specDiffRes.getUpdates());
        goodsRepository.deleteSpecs(specDiffRes.getDeletes());
        return true;
    }

    public boolean online(Integer goodsId, boolean online) {
        return goodsRepository.updateOnlineState(goodsId, online);
    }

    public boolean recommend(Integer goodsId, boolean recommend) {
        return goodsRepository.updateRecommend(goodsId, recommend);
    }

    public boolean deleted(Integer goodsId) {
        return goodsRepository.deleteGoods(goodsId);
    }


    /**
     * 更新商品销量
     *
     * @param goodsId
     * @param sales
     */
    public boolean updateGoodsSales(Integer goodsId, Integer sales) {
        return goodsRepository.updateSalesCnt(goodsId, sales);
    }

    /**
     * 列表查询
     *
     * @param req
     * @return
     */
    public ListRsp<GoodsInfoDto> queryFullGoodsList(GoodsQueryReq req) {
        List<GoodsEntity> goodsList = goodsRepository.listGoods(req);
        List<GoodsInfoDto> list = GoodsConvert.batchToGoodsInfo(goodsList, categoryService::getCategoryName);
        ListRsp<GoodsInfoDto> listRsp = ListRsp.newRsp(list, req.getPage(), req.getSize());
        if (req.getPage() == 1) {
            // 首页查询的时候，查一下总数
            listRsp.setTotal(goodsRepository.goodsCount(req));
        }
        return listRsp;
    }


    // -------------------- 下面是给app使用的前台service

    /**
     * 推荐商品列表
     *
     * @return
     */
    public List<GoodsInfoDto> queryRecommendList() {
        GoodsQueryReq req = new GoodsQueryReq();
        req.setRecommend(1);
        req.setPage(1);
        req.setSize(12);
        req.setState(GlobalConstants.GoodsApi.GOODS_ONLINE_STATE);
        List<GoodsEntity> list = goodsRepository.listSimpleGoods(req);
        return GoodsConvert.batchToGoodsInfo(list, null);
    }

    /**
     * 商品图强
     *
     * @return
     */
    public ListRsp<GoodsInfoDto> queryGoodsWall(GoodsQueryReq req) {
        req.autoInitReq();
        req.setState(GlobalConstants.ONLINE_STATE);
        List<GoodsEntity> entityList = goodsRepository.listSimpleGoods(req);
        List<GoodsInfoDto> resList = GoodsConvert.batchToGoodsInfo(entityList, null);
        return ListRsp.newRsp(resList, req.getPage(), req.getSize());
    }


    /**
     * 类目商品列表查询
     *
     * @param req
     * @return
     */
    public ListRsp<GoodsInfoDto> queryCategoryGoods(GoodsQueryReq req) {
        req.autoInitReq();
        req.setState(GlobalConstants.ONLINE_STATE);
        List<GoodsEntity> entityList = goodsRepository.listSimpleGoods(req);
        List<GoodsInfoDto> resList = GoodsConvert.batchToGoodsInfo(entityList, null);
        return ListRsp.newRsp(resList, req.getPage(), req.getSize());
    }

    /**
     * 商品详情
     *
     * @param goodsId
     * @return
     */
    public GoodsInfoDto queryGoodsDetail(Integer goodsId) {
        GoodsEntity goods = goodsRepository.queryGoodInfo(goodsId);
        return GoodsConvert.toGoodsInfo(goods, categoryService::getCategoryName);
    }
}
